﻿/******************************************************/
// RiceInfoDetailPage プレゼンテーションクラス  
/******************************************************/

using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using MAA.Business;
using System.Web.UI.WebControls;
using System.Text;

namespace MAA.Presentation.PRL.News
{
    public partial class RiceInfoDetailPage : System.Web.UI.Page
    {
        private ResourceManager rm = new ResourceManager("MAA.Presentation.language", System.Reflection.Assembly.GetExecutingAssembly());
        private CultureInfo m_JapanCulture = new CultureInfo("ja-JP");
        private CultureInfo m_VietnameCulture = new CultureInfo("vi");

        private void UpdateUI()
        {
            langBy.Text = rm.GetString("langBy");
            langOrigin.Text = rm.GetString("langOrigin");
            langDescription.Text = rm.GetString("langDescription");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["language"] == null)
            {
                Session["language"] = "vi";
            }
            if (Session["language"].Equals("vi"))
            {
                Thread.CurrentThread.CurrentUICulture = m_VietnameCulture;
                UpdateUI();
            }
            if (Session["language"].Equals("jp"))
            {
                Thread.CurrentThread.CurrentUICulture = m_JapanCulture;
                UpdateUI();
            }
            if (IsPostBack) return;

            int riceID = Convert.ToInt32(Request.QueryString["riceID"]);

            BOLNews bolNews = new BOLNews();
            DataTable tblRice = new DataTable();
            string message = "";
            string lang = Session["language"].ToString();
            // View detail information of a rice
            if (bolNews.viewRiceInfoDetail(lang,riceID, out tblRice, out message) == true)
            {
                if (tblRice.Rows.Count > 0)
                {
                    for (int i = 0; i < tblRice.Rows.Count; ++i)
                    {
                        lblTypeName.Text = Convert.ToString(tblRice.Rows[i]["TypeName"]);
                        lblPosterName.Text = Convert.ToString(tblRice.Rows[i]["Name"]);
                        impPicture.ImageUrl = Convert.ToString(tblRice.Rows[i]["Image"]);
                        lblOrigin.Text = Convert.ToString(tblRice.Rows[i]["Origin"]);
                        string des = Convert.ToString(tblRice.Rows[i]["Description"]);
                        StringBuilder sb = new StringBuilder(des);
                        lblDescription.Text = sb.ToString().Replace("\n", "<br />");
                    }
                }
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
                    "Alert", "<script>alert('" + PRLCommon.ChangeLanguageMessage(message) + "')</script>", false);
            }

            DataTable tblRiceRandom = new DataTable();
            // View information of more 4 random rice type
            if (bolNews.viewRiceInfoRandom(lang,riceID,out tblRiceRandom, out message) == true)
            {
                DataView riceDataView = new DataView(tblRiceRandom);
                PagedDataSource riceItems = new PagedDataSource();
                riceItems.DataSource = riceDataView;
                tblRiceInfo.DataSource = riceItems;
                tblRiceInfo.DataBind();
            }
            else
            {
                tblRiceInfo.DataSource = null;
                tblRiceInfo.DataBind();
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
                    "Alert", "<script>alert('" + PRLCommon.ChangeLanguageMessage(message) + "')</script>", false);
            }
        }

        protected void tblRiceInfo_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label langOrigin = (Label)e.Item.FindControl("langOrigin");
                Label langDes = (Label)e.Item.FindControl("langDes");
                langOrigin.Text = rm.GetString("langOrigin");
                langDes.Text = rm.GetString("langDescription");
            }
        }
    }
}